<?php

class Convert extends Module
{
    function __construct()
    {
    }
    
    private function _getStyles()
    {
        return array( 'json_decode', 'json_encode', 'unserialize', 'date'
            , 'strtodate', 'strtotime', 'urldecode', 'urlencode', 'toparam'
            ,'toline','tolines', 'getKey', 'getUrlContent', 'count' );
    }
    
    function convert()
    {
        $ret = array('ret'=>true, 'info'=>'成功.');
        $style = isset( $_POST['style'] ) ? trim( $_POST['style'] ) : 'json';
        $datain = isset( $_POST['datain'] ) ? trim( $_POST['datain'] ) : '';
        switch( $style )
        {
            case 'json_decode':
                $ret['data'] = print_r( json_decode( $datain, true ), true );
                break;
            case 'json_encode':
                $ret['data'] = print_r( json_encode( $datain ), true );
                break;
            case 'unserialize':
                $ret['data'] = print_r( unserialize( $datain ), true );
                break;
            case 'strtotime':
                $ret['data'] = print_r( strtotime( $datain ), true );
                break;
            case 'date':
                $ret['data'] = print_r( date( 'Y-m-d H:i:s', $datain ), true );
                break;
            case 'strtodate':
                $ret['data'] = print_r( date( 'Y-m-d H:i:s', strtotime( $datain ) ), true );
                break;
            case 'urldecode':
                $ret['data'] = print_r( urldecode( $datain ), true );
                break;
            case 'urlencode':
                $ret['data'] = print_r( urlencode( $datain ), true );
                break;
            case 'toparam':
                $ret['data'] = print_r( $this->toParam( $datain ), true );
                break;
            case 'toline':
                $ret['data'] = print_r( $this->toLine( $datain ), true );
                break;
            case 'tolines':
                $ret['data'] = print_r( $this->toLines( $datain ), true );
                break;
            case 'getKey':
                $ret['data'] = print_r( $this->getKey( $datain ), true );
                break;
            case 'getUrlContent':
                $ret['data'] = $this->getUrlContent( $datain );
                break;
            case 'count':
                $ret['data'] = date('Y-m-d H:i:s');
                $page = 0;
                $cnt = 10;
                $arr = array();
                if( preg_match( '/&page=([0-9]*)/', $datain, $arr ) && $arr[1] )
                {
                    $page = $arr[1];
                }
                if( preg_match( '/&cnt=([0-9]*)/', $datain, $arr ) && $arr[1] )
                {
                    $cnt = $arr[1];
                }
                for( $i=1; $i<=$cnt; $i++ )
                {
                    $s = $this->countComments( $datain, $i+$page );
                    if( $s )
                    {
                        $ret['data'] .= $s;
                    }
                    else
                    {
                        break;
                    }
                }
                break;
            default:
                $ret['ret'] = false;
                $ret['info'] = "找不到这种类型;";
                break;
        }
        
        /*
        Logs::init( 'log' );
        Logs::info( $ret );
        Logs::info( $_POST );
        //*/
        
        echo json_encode( $ret );
    }
    
    private function countComments( $url, $page )
    {
        $url = preg_replace( '/&page=[0-9]*/', '&page='.$page, $url );
        $str = file_get_contents( $url );
        if( $str == false ) return;
        $arr = json_decode( $str, true );
        if( $arr == false ) return;
        $info = "\np=".$page;
        if( isset( $arr['comments']['hot'] ) )
        {
            $info .= "hot:".count( $arr['comments']['hot'] ).",";
        }
        if( isset( $arr['comments']['new'] ) && $arr['comments']['new'] )
        {
            $info .= "new:".count( $arr['comments']['new'] ).",";
            foreach( $arr['comments']['new'] as $one )
            {
                $lastone = end( $one );
                $info .= number_format( $lastone['reply_id'], 0, '', '' ).",";
            }
        }
        else
        {
            return;
        }
        return $info;
    }
    
    private function getUrlContent( $str )
    {
        $this->info( $str );
        if( preg_match( '/^http.*?([^\/]*)$/', $str, $arr ) )
        {
            $url = $str;
            $str = $arr[1];
        }
        else
        {
            $url = "http://view.inews.qq.com/a/{$str}";
        }
        
        if( preg_match( '/^([A-Z]*).*$/', $str, $arr ) )
        {
            $site = $arr[1];
        }
        else
        {
            $this->info( "can't find site.{$str}" );
        }
        $this->info( print_r( $arr, true ) );
        
        $info = file_get_contents( $url );
        if( preg_match( '/cmt_id = ([0-9]+)/', $info , $arr ) )
        {
            $cmt_id = $arr[1];
        }
        else
        {
            $this->info( "false, can't find it." );
            return;
        }
        $ret = " http://inews.qq.com/getQQNewsComment?page=1&comment_id={$cmt_id}&url="
            .urlencode($url)."&chlid=news_news_tech&article_id={$str}"
            ."\n".$url
            ."";
        return $ret;
    }
    
    private function getKey( $str )
    {
        //uin=962863684&award=202&key=ZITHVAMWRY
        // uin=371078926&award=205&key=ZITHVAMWRY&actid=1249669282
        $arr = array();
        parse_str( $str, $arr );
        $hash = md5( trim( $arr['uin'], 'o0' ) . $arr['award'] . $arr['key'] );
        $info = "http://icar.qq.com/updateActState?uin={$arr['uin']}&award={$arr['award']}&hash={$hash}\n";
        
        $hash = md5( trim( $arr['uin'], 'o0' ) . $arr['key'] );
        $info .= "http://icar.qq.com/judgeActRight?uin={$arr['uin']}&hash={$hash}\n";
        $info .= "http://icar.qq.com/clearActState?uin={$arr['uin']}&hash={$hash}\n";
        
        $hash = md5( date("Y-m-d") . '371078926' );
        $info .= "http://icar.qq.com/addActState?qq={$arr['uin']}&actid={$arr['actid']}&istest={$hash}\n";
        
        $hash = md5( date("Y-m-d") . '371078926' );
        $info .= "http://icar.qq.com/infoActState?actid={$arr['actid']}&istest={$hash}\n";
        return $info;
    }
    
    private function toParam( $str )
    {
        $arr = explode( "\n", $str );
        foreach( $arr as $k=>&$one )
        {
            $atmp = array();
            if( preg_match( '/^\s*\[(.*)\]\s*=>\s*([^\s]*)\s*$/', $one, $atmp ) )
            {
                $one = $atmp[1]."=".urlencode( $atmp[2] );
            }
        }
        return implode( '&', $arr );
    }
    
    private function toLine( $str )
    {
        $lines = explode( "\n", $str );
        foreach( $lines as &$line )
        {
            $line = trim( $line );
            if( substr( $line, -4 ) == 'then' 
                || substr( $line, -1 ) == '{'
                || substr( $line, -1 ) == '}'
                || substr( $line, -1 ) == ')'
                || substr( $line, -2 ) == 'do' )
            {
                $line .= " ";
            }
            else
            {
                $line .= ";";
            }
        }
        $str = implode( "", $lines );
        
        $arr = array(
            "  "=>" ",
            " ;"=>";",
            "; "=>";",
        );
        foreach( $arr as $k=>$v )
        {
            $str = str_replace( $k, $v, $str );
        }
        return $str;        
    }
    
    
    private function toLines( $str )
    {        
        $arr = array(
            "{"=>"\n{\n",
            ";"=>";\n",
            "}"=>"\n}\n",
        );
        foreach( $arr as $k=>$v )
        {
            $str = str_replace( $k, $v, $str );
        }
        return $str;
    }
    
    public function display()
    {
        $ret = array('ret'=>true, 'info'=>'succes');
        $ret['data'] = $this->_getStyles();
        parent::smartyOut('convert', $ret);
    }
    
    
    // 腾讯网主力站点映射
    private function getSiteId( $site )
    {
        $siteMap = array(
                'astro'=> 'AST',
                'edu'=> 'EDU',
                'ent'=> 'ENT',
                'gamezone'=> 'GAM',
                'games' => 'GAM',
                'joke'=> 'JOK',
                'search'=> 'SEA',
                'news'=> 'NEW',
                'sports'=> 'SPO',
                'auto'=> 'AUT',
                'www'=> 'WWW',
                'img1'=> 'IM1',
                'panel'=> 'PAN',
                'bbs'=> 'BBS',
                'uppower'=> 'UPP',
                'webchat'=> 'WEB',
                'fashion'=> 'FAS',
                'discovery'=> 'DIS',
                'comic'=> 'COM',
                'imqq'=> 'IMQ',
                'sms'=> 'SMS',
                'mobile'=> 'MBL',
                'digi'=> 'DIG',
                'fm'=> 'FMM',
                'gter'=> 'GET',
                'olympic'=> 'OLY',
                'magazine'=> 'MAG',
                'minisite'=> 'MIN',
                'book'=> 'BOK',
                'music'=> 'MUS',
                'tech'=> 'TEC',
                'finance'=> 'FIN',
                'survey'=> 'SUR',
                'lady'=> 'LAD',
                'bb'=> 'VED',
                'sing'=> 'SIN',
                'weather'=> 'WEA',
                'service'=> 'SER',
                'musictop'=> 'MST',
                'zonetenpay'=> 'ZTP',
                'cq'=> 'CQC',
                'mat1'=> 'MAT',
                'tv'=> 'TVV',
                'luxury'=> 'LUX',
                'xian'=> 'XAC',
                'zhengzhou'=> 'ZZC',
                'view'=> 'VIW',
                'joy'=> 'JOY',
                '2006'=> '206',
                'flash'=> 'FLA',
                'gamezonealbum'=> 'GZA',
                'quan'=> 'QUA',
                'musictop2'=> 'MT2',
                '2008'=> '208',
                'pinglun'=> 'PLT',
                'comment1'=> 'CM1',
                'kid'=> 'KID',
                'stock'=> 'STC',
                'gongyi'=> 'GYY',
                'boaoforum'=> 'BAF',
                'cqsc'=> 'CQS',
                'baby'=> 'BAB',
                'lequipe'=> 'LQP',
                'cd'=> 'CDC',
                'xsoso'=> 'XSS',
                'download'=> 'DLD',
                'house'=> 'HOS',
                'cqwebapp'=> 'CQA',
                'qbar'=> 'QBR',
                'imgwap'=> 'IMW',
                'home3g'=> 'H3G',
                'expo'=> 'UED',
                'adver'=> 'ADV',
                'hb'=> 'HBC',
                'tencentmind'=> 'TCM',
                'mini2009'=> 'MN9',
                'pcdesk'=> 'PCD',
                'bbs1'=> 'BB1',
                'minixsoso'=> 'MNX',
                'paipai'=> 'PPP',
                'paipaibbs'=> 'PPB',
                '2010'=> '201',
                'paipailady'=> 'PPL',
                'paipaidigi'=> 'PPD',
                'paipaimai'=> 'PPM',
                'media'=> 'MDA',
                'fiba'=> 'FIB',
                'worldcup'=> 'WC0',
                'paipaigames'=> 'PPG',
                'microblog'=> 'MCB',
                'paipaiman'=> 'PPM',
                'paipailife'=> 'PPL',
                'fj'=> 'FJC',
                'gd'=> 'GDC',
                'i4s'=> 'I4S',
                'wbzt'=> 'WBZ',
                'qqusportnews'=> 'USN',
                'sh'=> 'SHC',
                'mb'=> 'MBB',
                'v'=> 'VDO',
                'ipad'=> 'IPD',
                '2012'=> '212',
                'app'=> 'APP',
                'cva'=> 'CVA',
                'mini2012'=> 'MN2',
                'henan'=> 'HNC',
                'juventus'=> 'JVT',
                'paipai3g'=> 'PP3',
                'mini'=> 'MNO',
                'hn'=> 'HNC',
                'rss'=> 'RSS',
                'news_photo'=> 'NPT',
                'news_hotweibo' => 'HWB',
                'hotweibo' => 'HWB',
                'mil' => 'MIL',
                '212' => '212',
                'wx' => 'WXN',
                'kn' => 'KNN',
                    );
        if( isset($siteMap[$site]))
        {
            return $siteMap[$site] ;
        }
        else
        {
            return False ;
        }
    }
}
